<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'user';   
        $this->load->model('Muser');
        $this->checkAdmin($this->session->userdata('admin_id'));
    }
    
    public function profile(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
		//$this->form_validation->set_rules('pword1', 'Mật khẩu mới', 'matches[pword2]');	
		//$this->form_validation->set_rules('pword2', 'Nhập lại mật khẩu mới', '');		
        $this->data['data'] = $this->Muser->getProfile($this->session->userdata('admin_id'));
        //echo '<pre>';die(print_r($this->data['data']));
        if($this->form_validation->run() === TRUE)
        {
			$username = $this->getParamString("username");
			$mobile = $this->getParamString("mobile");
			$fullname = $this->getParamString("fullname");
			$pword1 = $this->getParamString("pword1");
			$pword2 = $this->getParamString("pword2");
			if ( !in_array($pword1,array('', null)) && !in_array($pword2,array('', null)) && $pword1 != $pword2 ){
				die('<meta charset="utf-8"><script>alert("Mật khẩu không trùng nhau");window.location.href="'.$this->data['baseurl'].'profile.html";</script>');
			}
            $run = $this->Muser->updateProfileDb($username,$mobile,$fullname,$pword1,$pword2);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'profile.html";</script>');
            }
        }
        $this->data['action'] = 'profile';
        $this->load->view('template/layout', $this->data);
	}
    
    
    
    
    
    
    
    public function memberindex()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 20;
        $yearselectmonth = '';
        $this->data['year'] = $this->getParamString('year');
        $this->data['month'] = $this->getParamString('month');
        $this->data['selectmonth'] = $this->getmonth($this->data['month']);
        
        
        $this->data['page'] = $this->data['pageCurrent'] = $this->data['id_active'];
        $offset = (($this->data['id_active'] - 1) * $config['per_page']) > 0 ? (($this->data['id_active'] - 1) * $config['per_page']) : 0;
        $data = $this->Muser->getUser($config['per_page'],$offset,$this->data['year'],$this->data['month']);
        
        if(!in_array($this->getParamString('month'), array(null, '', '0')) && in_array($this->getParamString('year'), array(null, '', '0')))
        {
            $yearselectmonth = date("Y");
        }
        $this->data['selectyear'] = $this->getyear($this->data['year'] ? $this->data['year'] : $yearselectmonth);
        $config['base_url'] = base_url('member/');
        if(!in_array($this->getParamString('month'), array(null, '', '0')) || !in_array($this->getParamString('year'), array(null, '', '0')))
        {
            $config['paramhack'] = '?year='.$this->data['year'].'&month='.$this->data['month']; // Param custom by nhatnv
        }
        
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->data['id_active'];
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            if($this->_per->del != 4)
            {
                $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/member/index');
                exit();
            }
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Muser->deleteAcc($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/member/index";</script>');
            }
        }

        $this->data['action'] = 'memberindex';
        $this->load->view('template/layout',$this->data);
    }
    
    public function deletemember()
    {
            
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'manage/member/index');
            exit();
        }
        $idArray = $this->getParamUri(4,1);
        $run = $this->Muser->deleteAcc($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/member/index";</script>');
        }
    }
    
    
    public function addmember()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[member.email]');
        $this->form_validation->set_rules('pword', 'Mật khẩu', 'required|matches[pword2]');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Muser->addMemberDb($this->session->userdata('admin_id'));
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/member/index.html";</script>');
            }
        }
        $this->data['action'] = 'addmember';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editmember()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'inmanage/member/indexdex');
            exit();
        }
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->data['data'] = $this->Muser->getMemberid($this->data['id']);
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Muser->editMemberDb($this->session->userdata('admin_id'), $this->data['id']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/member/index.html";</script>');
            }
        }
        $this->data['action'] = 'editmember';
        $this->load->view('template/layout', $this->data);
    }
    public function exportuser()
    {
        $this->data['data'] = $this->Muser->getUserExport();
        $this->load->view('template/user/exportuser', $this->data);
    }
	
	public function exportusercoupon()
    {
        $this->data['data'] = $this->Muser->getUserExportCoupon();
        $this->load->view('template/user/exportusercoupon', $this->data);
    }
	
	public function exportusercode()
    {
        $this->data['data'] = $this->Muser->getUserExportCode();
        $this->load->view('template/user/exportusercode', $this->data);
    }
    private function getmonth($month)
    {
        $html = '';
        for($i = 1; $i <=12; $i++)
        {
            if($i < 10)
            {
                $val = '0'.$i;
            }else{
                $val = $i;
            }
            if($month == $val)
                {
                    $check = "selected='selected'";
                }else{
                    $check = "";
                }
            $html .= "<option value='".$val."'".$check.">Tháng ".$val."</option>";
        }
        return $html;
    }
    private function getyear($year)
    {
        $html = '';
        for($i = (date("Y") - 5); $i < (date("Y")); $i++)
        {
                if($year == $i)
                {
                    $check = "selected='selected'";
                }else{
                    $check = "";
                }
                $html .= "<option value='".$i."' ".$check." >Năm ".$i."</option>";
        }
        for($j = date("Y"); $j <= (date("Y") + 5); $j++)
        {
                if($year == $j)
                {
                    $check = "selected='selected'";
                }else{
                    $check = "";
                }
                $html .= "<option value='".$j."' ".$check." >Năm ".$j."</option>";
        }
        return $html;
    }
    private function selectGroupAcc($id)
    {
        $array = array(
                ""          => 'Chọn nhóm thành viên',
                "1"         => 'Administrator',
                "2"         => 'GvtechCom',
                "4"         => 'Member',
            );
        $html = '';
        foreach($array AS $le=>$el)
        {
            if($id == $le)
            {
                 $selected = " selected = 'selected' ";
            }else{
                $selected = "";
            }
            $html .= "<option value='".$le."' ".$selected.">".$el."</option>";
        }
        return $html;
    }
    
}
